package gameLogic;

class Board extends EventGenerator {
	BoardState[][] board;
	int turn;

	Board() {
		board = new BoardState[3][3];
		turn = 1;
		clear();
	}

	// makes a move on the board, generates an event 
	boolean makeMove(Move move) {
		if(isValid(move)) {
			System.out.println(turn+" "+move);
			board[move.y][move.x] = move.v;
			
			generateEvent(move);
			turn++;
			return true;
		}
		return false;
	}

	boolean isValid(Move move) {
		if (turn % 2 == 1 && move.v == BoardState.O)
			return false;
		else if (turn % 2 == 0 && move.v == BoardState.X)
			return false;
		
		return isEmpty(move.x, move.y);
	}
	
	boolean isEmpty(int x, int y) {
		return (board[y][x] == BoardState.EMPTY) ? true : false;
	}

	void clear() {
		for (int i = 0; i < 3; i++)
			for (int j = 0; j < 3; j++)
				board[i][j] = BoardState.EMPTY;
	}
	
	void print() {
		for (int i = 0; i < 3; i++) {
			for (int j = 0; j < 3; j++) {
				String s;
				if(board[i][j] == BoardState.EMPTY)
					s = "E";
				else
					s = (board[i][j] == BoardState.X) ? "X":"O";
				System.out.print(s);
			}
			System.out.println();
		}			
	}
}
